import { getToken } from '@/utils/storage'
import { baseUrl } from '@/config/resource'

const uploadFn = (filePath, type) => {
  return new Promise((resolve, reject) => {
    const urlObj = {
      image: '/api/upload/image',
      pic: '/api/upload/pic'
    }
    uni.uploadFile({
      url: baseUrl + urlObj[type],
      filePath,
      name: 'iFile',
      formData: {
        token: getToken()
      },
      success: res => {
        const newRes = res.data.replace('\uFEFF', '')
        const { data, msg } = JSON.parse(newRes)
        if (msg) {
          uni.showToast({
            title: msg,
            icon: 'none'
          })
        }
        resolve(data)
      },
      fail: err => {
        console.log('上传失败err', err)
        reject(err)
      }
    })
  })
}
export default uploadFn
